// pages/home/home.js

import Service from '../../model/service'
import Category from '../../model/category'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ['全部服务', '在提供', '正在找'],

        categoryList: [],


        serviceList: [],


        querypages: {
            page: 1,
    
            count: 5,
    
            category_id: 0,
    
            type: 0,
        },

        loading: true
    },
    // 总页数
    totalPage: 1,





    /**
 * 生命周期函数--监听页面加载
 */
    onLoad: async function (options) {
        await this._getCategoryList();
        await this.getServiceList();
        this.setData({
            loading: false
        })
    },



    async _getCategoryList() {
        const categoryList = await Category.getCategoryListWithAll();
        this.setData({
            categoryList: categoryList
        })
    },



    async getServiceList() {
        let {page , count , category_id , type} = this.data.querypages
        let res = await Service.reset().getServiceList(page , count , category_id , type);

        const total = res.total
        // 计算总页数
        this.totalPage = Math.ceil(total / this.data.querypages.count)

        // console.log(index);
        this.setData({
            serviceList: [...this.data.serviceList, ...res.data]
        })

        // 关闭下拉刷新的窗口
        wx.stopPullDownRefresh();

    },

    handleChangeTabIndex(e) {
        let index = e.detail.index
        this.data.querypages.type = index;
        this.setData({
            serviceList: [],
        })
        // console.log(this.data.serviceList);
        this.data.querypages.page = 1;
        this.getServiceList();
    },
    handleChangeCategoryId(e) {
        let id = e.detail.id
        this.data.querypages.category_id = id
        this.setData({
            serviceList: [],
        })
        // console.log(this.data.serviceList);
        this.data.querypages.page = 1;
        this.getServiceList();
    },



    onReachBottom: function () {
        // 判断还有没有下一页数据
        if (this.data.querypages.page >= this.totalPage) {
            wx.showToast({
                title: '没有下一页数据',
            });
            return
        } else {
            this.data.querypages.page++;
            this.getServiceList();
        }
    },

    handleSelectService(e){
        // console.log(e);
        wx.navigateTo({
            url: '/pages/service-detail/service-detail?service_id=' + e.currentTarget.dataset.service.id,
        });
          
    },


    /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
    onPullDownRefresh: function () {
        this.setData({
            serviceList: [],
        })
        // console.log(this.data.serviceList);
        this.data.querypages.page = 1;
        this.getServiceList();
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})